
import requests, time
from bs4 import BeautifulSoup

def req(url, direc = True):
    headersvalue = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50'
        }
    r = requests.get(url, headers=headersvalue)
    soup = BeautifulSoup(r.text, 'lxml')
    if direc:
        return soup
    else:
        return r

def jpg():
    t = time.localtime()
    jpg_name = 'd:/BingWallpaper--%s年%s月%s日' % (t.tm_year, t.tm_mon, t.tm_mday)
    soup = req('https://cn.bing.com/')
    img_url = soup.find('div', attrs={'class': 'img_cont'}).attrs['style'].split('(')[-1].split(')')[0]
    content = req('https://cn.bing.com' + img_url, direc=False).text
    with open(jpg_name, 'wb', encoding='utf-8') as f:
        f.write(content)
        print('今日份必应壁纸已下载，默认保存位置为 D 盘')

jpg()




